from wtforms import StringField,Form,IntegerField
from wtforms.validators import Length,InputRequired,ValidationError
from models.books import BoardModel

class bookForm(Form):
    name = StringField(validators=[Length(min=1,message="图书名字必须大于1")])
    desc = StringField(validators=[Length(min=8,max=200,message="图书简介字数在8--200之间")])
    board_id = IntegerField(validators=[InputRequired(message="请传入分类")])
    author_name = StringField(validators=[InputRequired(message="请输入作者姓名")])

    def validate_board_id(self,field):
        boards = BoardModel.query.filter_by(is_active=1).all()
        board_id = field.data
        bs_id = []
        for board in boards:
            bs_id.append(board.id)
        if board_id not in bs_id:
            raise ValidationError("board_id不在范围内")